package com.library.web.dashboard;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.library.annotation.Auth;
import com.library.utils.ResultUtils;
import com.library.utils.ResultVo;
import com.library.web.borrow_book.pojo.BorrowBook;
import com.library.web.borrow_book.service.BorrowBookService;
import com.library.web.sys_book.pojo.BookVo;
import com.library.web.sys_book.service.SysBooksService;
import com.library.web.sys_category.pojo.CategoryEcharts;
import com.library.web.sys_category.service.SysCategoryService;
import com.library.web.sys_notice.pojo.SysNotice;
import com.library.web.sys_notice.service.SysNoticeService;
import com.library.web.sys_reader.pojo.SysReader;
import com.library.web.sys_reader.service.SysReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 首页数据控制器
 */
@RestController
@RequestMapping("/api/dashBoard")
public class DashBoardController {
    @Autowired
    private SysReaderService sysReaderService;
    @Autowired
    private BorrowBookService borrowBookService;
    @Autowired
    private SysCategoryService categoryService;
    @Autowired
    private SysNoticeService sysNoticeService;
    @Autowired
    private SysBooksService booksService;

    //首页上的标签
    @GetMapping("/getAllTagCount")
    @Auth
    public ResultVo getAllTagCount(String userType, Long userId) {
        List<Integer> allTagCount = new ArrayList<>();
        //读者总数
        int readerCount = sysReaderService.count();
        allTagCount.add(readerCount);
        //读者待审核数
        QueryWrapper<SysReader> readerQuery = new QueryWrapper<>();
        readerQuery.lambda().eq(SysReader::getCheckStatus, "0");
        int readerWaitCount = sysReaderService.count(readerQuery);
        allTagCount.add(readerWaitCount);
        //借书待审核
        if (userType.equals("0")) {//读者
            QueryWrapper<BorrowBook> borrowQuery = new QueryWrapper<>();
            borrowQuery.lambda().eq(BorrowBook::getApplyStatus, "0")
                    .eq(BorrowBook::getReaderId, userId);
            int borrowWaitCount = borrowBookService.count(borrowQuery);
            allTagCount.add(borrowWaitCount);
        } else if (userType.equals("1")) {//管理员
            QueryWrapper<BorrowBook> borrowQuery = new QueryWrapper<>();
            borrowQuery.lambda().eq(BorrowBook::getApplyStatus, "0");
            int borrowWaitCount = borrowBookService.count(borrowQuery);
            allTagCount.add(borrowWaitCount);
        } else {
            int borrowWaitCount = 0;
            allTagCount.add(borrowWaitCount);
        }
        //到期代还书
        if (userType.equals("0")) {//读者
            QueryWrapper<BorrowBook> returnQuery = new QueryWrapper<>();
            returnQuery.lambda().eq(BorrowBook::getBorrowStatus, "1")
                    .lt(BorrowBook::getReturnTime, new Date()).eq(BorrowBook::getReaderId, userId);
            int returnCount = borrowBookService.count(returnQuery);
            allTagCount.add(returnCount);
        } else if (userType.equals("1")) {//管理
            QueryWrapper<BorrowBook> returnQuery = new QueryWrapper<>();
            returnQuery.lambda().eq(BorrowBook::getBorrowStatus, "1")
                    .lt(BorrowBook::getReturnTime, new Date());
            int returnCount = borrowBookService.count(returnQuery);
            allTagCount.add(returnCount);
        } else {
            int returnCount = 0;
            allTagCount.add(returnCount);
        }
        return ResultUtils.success("查询成功!", allTagCount);
    }

    //图书分类统计
    @Auth
    @GetMapping("/categoryCount")
    public ResultVo categoryCount() {
        CategoryEcharts vo = categoryService.getCategoryVo();
        return ResultUtils.success("查询成功", vo);
    }

    //热门图书列表
    @Auth
    @GetMapping("/getHotBook")
    public ResultVo getHotBook() {
        List<BookVo> hotBook = booksService.getHotBook();
        return ResultUtils.success("查询成功", hotBook);
    }

    //周图书列表
    @Auth
    @GetMapping("/getBookByWeek")
    public ResultVo getBookByWeek() {
        List<BookVo> getBookByWeek = booksService.getBookByWeek();
        return ResultUtils.success("查询成功", getBookByWeek);
    }

    //热门图书列表
    @Auth
    @GetMapping("/getBookByMonth")
    public ResultVo getBookByMonth() {
        List<BookVo> getBookByMonth = booksService.getBookByMonth();
        return ResultUtils.success("查询成功", getBookByMonth);
    }

    //通知列表
    @Auth
    @GetMapping("/getTopList")
    public ResultVo getTopList() {
        QueryWrapper<SysNotice> query = new QueryWrapper<>();
        query.lambda().orderByDesc(SysNotice::getCreateTime).last("limit 3");
        List<SysNotice> list = sysNoticeService.list(query);
        return ResultUtils.success("查询成功", list);
    }
}
